package com.sxp.controller;

import com.sxp.dao.RoleEnum;
import com.sxp.model.Month;
import com.sxp.service.MonthlyDetailService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class MonthlyDetailController extends HttpServlet {
    private static HttpSession session;
    private static final long serialVersionUID = 1L;
    private static String CURRENT_MONTH_DETAILS= "/currentmonthdetails.jsp";
    private static String NEXT_MONTH_DETAILS= "/nextmonthdetails.jsp";
    private static String PREVIOUS_MONTH_DETAILS= "/previousmonthdetails.jsp";
    private static String LOGIN = "/index.jsp";
    public MonthlyDetailService md;
    public Month month;


    public MonthlyDetailController() {
        super();
        md=new MonthlyDetailService();
        month=new Month();
        session = UserController.getSession();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String forward="";
        String action = request.getParameter("action");

        if(action.equalsIgnoreCase("currentmonthlydetails")) {
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                forward = CURRENT_MONTH_DETAILS;
                request.setAttribute("year",month.getCurrentYear());
                request.setAttribute("month",month.getCurrentMonth());
                request.setAttribute("days",month.getCurrentMonthMaximumDays());
                request.setAttribute("mealTypeCountByDateMap",md.getCurrentMonthMealCountMap());
                request.setAttribute("riceTypeCountByDateMap",md.getCurrentMonthRiceCountMap());
                request.setAttribute("totalMap",md.getCurrentMonthTotalByDate());
            }else {
                forward = LOGIN;
            }
        }
        if(action.equalsIgnoreCase("previousmonthlydetails")) {
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                forward = PREVIOUS_MONTH_DETAILS;
                request.setAttribute("year",month.getCurrentYear());
                request.setAttribute("month",month.getCurrentMonth());
                request.setAttribute("days",month.getPreviousMonthMaximumDays());
                request.setAttribute("mealTypeCountByDateMap",md.getPreviousMonthMealCountMap());
                request.setAttribute("riceTypeCountByDateMap",md.getPreviousMonthRiceCountMap());
                request.setAttribute("totalMap",md.getPreviousMonthTotalByDate());
            }else {
                forward = LOGIN;
            }

        }
        if(action.equalsIgnoreCase("nextmonthlydetails")) {
            if(session.getAttribute("username") != null && session.getAttribute("role") == RoleEnum.ADMIN) {
                forward = NEXT_MONTH_DETAILS;
                request.setAttribute("days",month.getNextMonthMaximumDays());
                request.setAttribute("year",month.getCurrentYear());
                request.setAttribute("month",month.getCurrentMonth());
                request.setAttribute("mealTypeCountByDateMap",md.getNextMonthMealCountMap());
                request.setAttribute("riceTypeCountByDateMap",md.getNextMonthRiceCountMap());
                request.setAttribute("totalMap",md.getNextMonthTotalByDate());
            }else {
                forward = LOGIN;
            }
        }
        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}
